<?php
class Order extends Home_Controller{
	public function __construct(){
		parent::__construct();
		$this->load->Model(array("Mcatalogue_product","Mproduct","Mcase_product","Morder"));
	}	
	
	public function index(){
		$url = $this->uri->segment(2);
		$data['product'] = $this->Mproduct->getProductByUrl($url);
		$data['case'] = $this->Mcase_product->getCaseByUrl($url);
		$data['cata_name'] = $this->Mcatalogue_product->getNameByUrl($url);
		$data['template'] = 'order/index';
		if($this->input->server('REQUEST_METHOD') == "POST"){			
			$dataInsert = array(
				'fullname' => $this->input->post('order-fullname'),
				'email' => $this->input->post('order-email'),
				'phone' => $this->input->post('order-phone'),
				'address' => $this->input->post('order-address'),
				'note' => $this->input->post('order-note'),
				'date_create' => date('Y-m-d H:i:s',time()), 
			);
			$this->Morder->insert($dataInsert);
			$order_id = $this->db->insert_id();
			
			$dataImage = array();
			if (!empty($_FILES['imageProduct'])) {
				
				$idImageRemove = $this->input->post('idImageRemove');
				if($idImageRemove != ''){
					$arrIdRemove = explode(',',$this->input->post('idImageRemove'));
					foreach($arrIdRemove as $iIR){
						if(trim($iIR) != ''){
							unset($_FILES['imageProduct']['name'][$iIR]);
							unset($_FILES['imageProduct']['tmp_name'][$iIR]);
							unset($_FILES['imageProduct']['type'][$iIR]);
							unset($_FILES['imageProduct']['error'][$iIR]);
							unset($_FILES['imageProduct']['size'][$iIR]);
						}
					}
				}
				
				$path_upload = './assets/filemanager/imageUpload/'.$order_id.'/';
				if (!is_dir($path_upload)) {
					mkdir($path_upload);
				}
				$config['upload_path'] = $path_upload;
				$config['allowed_types'] = 'gif|jpg|png';
				$config['encrypt_name'] = TRUE;
				$config['quality'] = '100%';
				$this->load->library('upload', $config);
				$this->upload->do_multi_upload('imageProduct');
				$source = $this->upload->get_multi_upload_data();
				foreach($source as $img){
					$dataImage[] = $path_upload.$img['file_name'];
				}
			}
			
			$dataInsert = array(
				'pro_id' => $this->input->post('product'),
				'case_id' => serialize($this->input->post('model')),
				'image' => serialize($dataImage),
				'order_id' => $order_id, 
			);
			$this->Morder->insertDetail($dataInsert);
			
//			$this->load->library('email');
//			$config['protocol'] = "smtp";
//			$config['smtp_host'] = "ssl://smtp.gmail.com";
//			$config['smtp_port'] = "465";
//			$config['smtp_user'] = "khoinguyen.net.vn@gmail.com"; 
//			$config['smtp_pass'] = "60@111.79";
//			$config['charset'] = "utf-8";
//			$config['mailtype'] = "html";
//			$config['newline'] = "\r\n";
//
//			$this->email->initialize($config);
//
//			$this->email->from('khoinguyen.net.vn@gmail.com', 'Info');
//			$this->email->to($this->input->post('order-email'));
//	
//			$subject = '';
//			$message = '';
//
//			$this->email->subject($subject);
//			$this->email->message($message);
//
//			$this->email->send();
			
			$this->session->set_userdata("flagOrderSuccess",1);
			redirect(base_url()."dat-hang/thanh-cong?order_id=".$order_id);
		}
		$this->load->view("home/template",$data);	
	}
	
	public function success(){
		if(isset($_GET['order_id'])){
			$id = $_GET['order_id'];
			if(is_numeric($id)){
				if($this->session->userdata('flagOrderSuccess') == 1){
					$this->session->set_userdata("flagOrderSuccess",0);
					$data['template'] = 'order/success';
					$this->load->view("home/template",$data);
				}else{
					redirect(base_url());
				}
			}else{
				redirect(base_url());
			}	
		}else{
			redirect(base_url());
		}
	}
}